//
//  HMEmoticonKeyBoard.swift
//  Weibo22
//
//  Created by Apple on 16/12/10.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit

/*
    1. 表情视图 -> UICollectionView
    2. 页数指示器 -> UIPageControl
    3. toolbar -> UIStackView
 
 */
//  重用标记
private let HMEmoticonCollectionViewCellIdentifier = "HMEmoticonCollectionViewCellIdentifier"

//  自定义表情键盘视图
class HMEmoticonKeyBoard: UIView {
    
    //  MARK: -- 懒加载
    fileprivate lazy var toolBar: HMEmoticonToolBar = HMEmoticonToolBar()
    //  表情视图
    fileprivate lazy var emoticonCollectionView: UICollectionView = {
    
        let flowLayout = UICollectionViewFlowLayout()
        //  水平滚动
        flowLayout.scrollDirection = .horizontal
        
        
        let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: flowLayout)
        collectionView.backgroundColor = self.backgroundColor
        
        //  开启分页
        collectionView.isPagingEnabled = true
        //  滚动指示器隐藏
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        //  取消弹簧效果
        collectionView.bounces = false
        
        //  注册cell
        collectionView.register(HMEmoticonCollectionViewCell.self, forCellWithReuseIdentifier: HMEmoticonCollectionViewCellIdentifier)
        //  设置数据源代理
        collectionView.dataSource = self
        //  设置代理
        collectionView.delegate = self
        
        return collectionView
        
    }()
    
    //  页数指示器
    fileprivate lazy var pageControl: UIPageControl = {
        let pageCtr = UIPageControl()
        //  选中指示器的颜色
        pageCtr.currentPageIndicatorTintColor = UIColor(patternImage: UIImage(named: "compose_keyboard_dot_selected")!)
        //  未选中的指示器颜色
        pageCtr.pageIndicatorTintColor = UIColor(patternImage: UIImage(named: "compose_keyboard_dot_normal")!)
        
        //  隐藏单页显示
        pageCtr.hidesForSinglePage = true
        return pageCtr
    
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //  添加控件设置约束
    private func setupUI() {
        
        //  默认滚动到第二种表情
        let normalIndexPath = IndexPath(item: 0, section: 1)
        //  主线程异步,需要等到其它视图创建完成和加载完数据以后然后在滚动指定的indexPath
        DispatchQueue.main.async {
            //  滚动指定的indexPath
            self.emoticonCollectionView.scrollToItem(at: normalIndexPath, at: .left, animated: false)
            //  设置页数
            self.setPageControlData(indexPath: normalIndexPath)
        }
       
        
        
        
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        addSubview(emoticonCollectionView)
        addSubview(pageControl)
        addSubview(toolBar)
        
        emoticonCollectionView.snp_makeConstraints { (make) in
            make.top.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.bottom.equalTo(toolBar.snp_top)
        }
        
        pageControl.snp_makeConstraints { (make) in
            make.bottom.equalTo(emoticonCollectionView)
            make.centerX.equalTo(emoticonCollectionView)
            make.height.equalTo(10)
        }
        
        toolBar.snp_makeConstraints { (make) in
            make.bottom.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.height.equalTo(35)
        }
        //  使用self注意循环引用
        toolBar.callBack = { [weak self] (type)-> Void in
            
            
            let indexPath: IndexPath
            switch type {
            case .recent:
                print("最近")
                indexPath = IndexPath(item: 0, section: 0)
            case .normal:
                print("默认")
                indexPath = IndexPath(item: 0, section: 1)
            case .emoji:
                print("emoji")
                indexPath = IndexPath(item: 0, section: 2)
            case .lxh:
                print("浪小花")
                indexPath = IndexPath(item: 0, section: 3)
            }
            //  不要开启动画
            self?.emoticonCollectionView.scrollToItem(at: indexPath, at: .left, animated: false)
            //  设置页数
            self?.setPageControlData(indexPath: indexPath)
            
        }
        
        
    }
    
    //  重写加载最近表情这组数据
    func reloadRecentEmotionData() {
        
        let recentIndexPath = IndexPath(item: 0, section: 0)
        emoticonCollectionView.reloadItems(at: [recentIndexPath])
    
    }
    
    
    //  设置页数指示器的数据
    fileprivate func setPageControlData(indexPath: IndexPath) {
        
        //  设置页数
        pageControl.numberOfPages = HMEmoticonTools.sharedTools.allEmoticonArray[indexPath.section].count
        //  当前滚动页数索引
        pageControl.currentPage = indexPath.item
    }
    
    //  设置每项的大小
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let flowLayout = emoticonCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
        //  每项的大小等于当前表情视图的大小
        flowLayout.itemSize = emoticonCollectionView.size
        //  设置每项之间的间距
        flowLayout.minimumLineSpacing = 0
        flowLayout.minimumInteritemSpacing = 0
        
    }
    
    
}

extension HMEmoticonKeyBoard: UICollectionViewDataSource, UICollectionViewDelegate {
    
    //  返回多少组
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return HMEmoticonTools.sharedTools.allEmoticonArray.count
    }
    
    //  每组显示多少页 (cell)
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return HMEmoticonTools.sharedTools.allEmoticonArray[section].count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: HMEmoticonCollectionViewCellIdentifier, for: indexPath) as! HMEmoticonCollectionViewCell
        
        //  表情数组
        let emoticonArray = HMEmoticonTools.sharedTools.allEmoticonArray[indexPath.section][indexPath.item]
        cell.emoticonArray = emoticonArray
        
        cell.indexPath = indexPath
        return cell
    }
    
    //  监听表情视图的滚动
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        //  水平方向的内容中心点
        let contentCenterX = emoticonCollectionView.width / 2 + emoticonCollectionView.contentOffset.x
        //  如果是垂直方向滚动,那么垂直方向的中心点 = emoticonCollectionView.height / 2 + emoticonCollectionView.contentOffset.y
        let contentCenterY = emoticonCollectionView.height / 2
        
        //  表情视图内容中心点
        let contentCenter = CGPoint(x: contentCenterX, y: contentCenterY)
        //  根据内容中心点获取对应的indexpath
        if let indexPath = emoticonCollectionView.indexPathForItem(at: contentCenter) {
            //  获取对应的section
            let section = indexPath.section
            
            toolBar.selectedButton(section: section)
            //  设置页数
            setPageControlData(indexPath: indexPath)
            
        }
        
        
        
    }
    
    

}






